﻿using System;
using System.Collections.Generic;
using ZX.Web.UI.Controls;
using ZX.DBModule;
using ZX.BLL.Extend;
using ZX.CRM.Bussiness;
using System.Data;
using ZX.Report;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Web.UI;
namespace ZX.WebApp.DeskTop.DeskTopItem
{
    public partial class ChanelTypeReport : BasePage
    {
        string htmlTemp = "";
        BCrmReport BReport = new BCrmReport();   //数据业务类
        protected void Page_Load(object sender, EventArgs e)
        {

            try
            {
                DataSet dataSources = null;
                Dictionary<int, string> titles = new Dictionary<int, string>();
                Dictionary<int, int> widths = new Dictionary<int, int>();
                Dictionary<int, int> heights = new Dictionary<int, int>();
                Dictionary<int, string> viewTypes = new Dictionary<int, string>();
                Dictionary<int, int> xyType = new Dictionary<int, int>();
                List<int> seqNum = new List<int>();
                DataTable source = null;
                seqNum.Add(1);
                titles.Add(1, "");
                System.Drawing.Size mSize = SystemInformation.WorkingArea.Size;
                int ii = mSize.Width = 470;

                widths.Add(1, ii);
                heights.Add(1, 180);
                viewTypes.Add(1, "bar");//柱状显示
                xyType.Add(1, 1);
                DataTable dt = null;
                string strDate = Request["date"];
                string startDate = DateTime.Now.AddDays(-DateTime.Now.Day + 1).ToShortDateString();
                string endDate = DateTime.Now.ToShortDateString();
                if (strDate == "month")
                {
                    startDate = DateTime.Now.AddDays(-DateTime.Now.Day + 1).ToShortDateString(); endDate = DateTime.Now.AddMonths(1).AddDays(-DateTime.Now.Day).ToShortDateString();
                }
                else if (strDate == "season")
                {
                    int season = DateTime.Now.Month % 3;
                    if (season == 0)
                    {
                        startDate = DateTime.Now.Year.ToString() + "-" + (DateTime.Now.Month - 2).ToString() + "-01";
                        endDate = DateTime.Now.AddMonths(1).AddDays(-DateTime.Now.Day).ToShortDateString();
                    }
                    else if (season == 1)
                    {
                        startDate = DateTime.Now.AddDays(-DateTime.Now.Day + 1).ToShortDateString(); endDate = DateTime.Now.AddMonths(3).AddDays(-DateTime.Now.Day).ToShortDateString();
                    }
                    else if (season == 2)
                    {
                        startDate = DateTime.Now.Year.ToString() + "-" + (DateTime.Now.Month - 1).ToString() + "-01";
                        endDate = DateTime.Now.AddMonths(2).AddDays(-DateTime.Now.Day).ToShortDateString();
                    }
                }
                else if (strDate == "year")
                {
                    startDate = DateTime.Now.Year.ToString() + "-01-01";
                    endDate = DateTime.Now.Year.ToString() + "-12-31";
                }
                else
                {
                    startDate = DateTime.Now.AddDays(-DateTime.Now.Day + 1).ToShortDateString();
                    endDate = DateTime.Now.AddMonths(1).AddDays(-DateTime.Now.Day).ToShortDateString();
                }

                dt = BReport.ReturnSaleRoad(new Guid(this.UserId.ToString()), startDate, endDate, getPowerType);
                if (dt == null || dt.Rows.Count == 0) 
                {
                    lblEpm.Text = "<br><font color='red'>&nbsp;&nbsp;&nbsp;&nbsp;没有数据</font>";
                    this.lblTitle.Visible = false;
                    this.lblTotalMoney.Visible = false;
                    this.lblEpm.Visible = false;
                    return; 
                }
                source = dt.Copy();
                decimal totalMoney = 0;
                for (int i = 0; i < source.Rows.Count; i++)
                {
                    totalMoney = totalMoney + (decimal)source.Rows[i][1];
                    string deptName = source.Rows[i][0].ToString();
                    source.Rows[i][0] = deptName;
                }
                lblTotalMoney.Text = "<font color='red'>" + totalMoney.ToString() + "万</font>";
                source.Columns[1].Caption = "单位：万元";
                source.TableName = "1";
                dataSources = new DataSet();
                dataSources.Tables.Add(source);
                if (dataSources == null || dataSources.Tables.Count==0)
                {
                    lblEpm.Text = "<br><font color='red'>&nbsp;&nbsp;&nbsp;&nbsp;没有数据</font>";
                    this.lblTitle.Visible = false;
                    this.lblTotalMoney.Visible = false;
                    //this.lblEpm.Visible = false;
                    return;
                }
                ChartParams paramsFactory = (ChartParams)ParamsFactory.GetInstance(ReportType.Chart, dataSources);
                paramsFactory.Title = titles;
                paramsFactory.Width = widths;
                paramsFactory.Height = heights;
                paramsFactory.SeqNum = seqNum.ToArray();
                paramsFactory.ChartViewType = viewTypes;
                paramsFactory.XYType = xyType;
                ReportFactory reportFactory = ReportFactory.GetInstance(paramsFactory);
                this.ReportViewer.Report = reportFactory.GetReport();
            }
            catch (Exception ex)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "script", "<script language='javascript'>alert('生成报表出现错误:" + ex.Message + "');</script>");
            }
        }

        private int getPowerType
        {
            get
            {
                int powertype = 0;
                if (Request["PowerType"] == null || Request["PowerType"] == "" || Request["PowerType"] == "0")
                    powertype = 0;
                else
                    powertype = 1;
                return powertype;
            }
        }
        private string Obj
        {
            get
            {
                string obj = Request["obj"];
                return obj;
            }
        }
        public string url
        {
            get
            {
                string url = Request["url"];
                return url;
            }
        }
    }
}